package cn.mdmm.controller.type;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mdmm.common.MessageConstant;
import cn.mdmm.controller.BaseController;
import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.entity.system.SysUser;
import cn.mdmm.entity.type.TypeInfoModel;
import cn.mdmm.entity.type.dto.TypeTree;
import cn.mdmm.service.system.SysDictService;
import cn.mdmm.service.type.TypeInfoService;
import cn.modoumama.common.annotation.OpLog;
import cn.modoumama.common.exception.ServiceException;
import cn.modoumama.common.exception.SystemException;
import cn.modoumama.common.utils.JSONUtils;
import cn.modoumama.page.GridDataModel;

@Controller
@RequestMapping(value = "/type")
@OpLog(logDesc = "类型管理")
public class TypeController extends BaseController {
	@Autowired
	TypeInfoService typeInfoService;
	@Autowired
	private SysDictService sysDictService;

	@RequestMapping(value = "init")
	public String init(HttpServletRequest request, Integer typeId) {
		request.setAttribute("typeId", 0);
		Map<String, String> statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusMap", JSONUtils.toJson(statusMap));
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		return "type/init";
	}

	@RequestMapping(value = "tree")
	@ResponseBody
	public List<TypeTree> tree(HttpServletRequest request, Integer id) {
		id = (id == null || id < 0) ? 0 : id;
		return typeInfoService.getTypeTree(id, false, true);
	}

	/**
	 * 获取类型列表
	 * 
	 * @param request
	 * @param response
	 * @param title
	 * @param newsType
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	@OpLog(logDesc = "类型列表")
	public GridDataModel findByConditions(HttpServletRequest request, Integer typeId) {
		Integer id = (typeId == null || typeId < 1) ? 1 : typeId;
		List<TypeInfoModel> types = typeInfoService.getOnlyChildrenTypeById(id);
		GridDataModel gridDataModel = null;
		if (types == null) {
			gridDataModel = new GridDataModel(types, 0);
		} else {
			gridDataModel = new GridDataModel(types, types.size());
		}
		return gridDataModel;
	}

	@RequestMapping(value = "showAdd")
	public String showAdd(HttpServletRequest request) {
		Integer pid = Integer.valueOf(request.getParameter("pid"));
		TypeInfoModel parent = typeInfoService.getTypeById(pid);
		if (parent == null) {
			parent = new TypeInfoModel();
			parent.setId(0);
			parent.setName("总分类");
		}

		request.setAttribute("parent", parent);
		Map<String, String> statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		return "type/add";
	}

	@RequestMapping(value = "insert")
	public String insert(HttpServletRequest request, TypeInfoModel typeInfoModel) {
		typeInfoService.insert(typeInfoModel);
		return "success";
	}

	@RequestMapping(value = "showEdit")
	public String showEdit(HttpServletRequest request, Integer id) {
		TypeInfoModel type = typeInfoService.getTypeById(id);
		TypeInfoModel parent = typeInfoService.getTypeById(type.getPid());
		request.setAttribute("type", type);
		request.setAttribute("parent", parent);
		Map<String, String> statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		return "type/edit";
	}

	@RequestMapping(value = "update")
	public String update(HttpServletRequest request, TypeInfoModel typeInfoModel) {
		typeInfoService.mergeById(typeInfoModel);
		return "success";
	}

	@RequestMapping(value = "delete")
	@ResponseBody
	public ResponseBodyToOne delete(HttpServletRequest request, Integer[] ids) {
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		logger.debug("method: delete() ");
		try {
			typeInfoService.removeByIds(ids);
		} catch (Exception e) {
			throw new ServiceException(MessageConstant.SYSTEM_ERROR);
		}
		return responseBody;
	}

	@RequestMapping(value = "checkType")
	@ResponseBody
	public ResponseBodyToOne checkUserName(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TypeInfoModel typeInfoModel) {
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		Map<String, Object> condititon = new HashMap<String, Object>();
		condititon.put("name", typeInfoModel.getName());
		condititon.put("englishName", typeInfoModel.getEnglishName());
		List<TypeInfoModel> types = typeInfoService.findModelsByCondition(condititon);
		if (types != null && types.size() > 0) {
			throw new SystemException("字段已存在");
		}
		return responseBody;
	}
}
